package mt.dummy;

import java.util.LinkedList;

import javax.xml.ws.handler.MessageContext;

import mt.Order;
import mt.comm.ClientComm;
import mt.comm.ClientSideMessage;

public class ClientCommDummy implements ClientComm {

	LinkedList<ClientSideMessageDummy> clientMessages;
	
	 public ClientCommDummy() {	
		 clientMessages = new LinkedList<ClientSideMessageDummy>();
		 createTestMessages();
		 
	}
	
	
	
	
	private void createTestMessages() {
		
			
			
			ClientSideMessageDummy msg1 = new ClientSideMessageDummy(ClientSideMessage.Type.ORDER);
			ClientSideMessageDummy msg2 = new ClientSideMessageDummy(ClientSideMessage.Type.ORDER);
			ClientSideMessageDummy msg3 = new ClientSideMessageDummy(ClientSideMessage.Type.ORDER);
			ClientSideMessageDummy msg4 = new ClientSideMessageDummy(ClientSideMessage.Type.ORDER);
			ClientSideMessageDummy msg5 = new ClientSideMessageDummy(ClientSideMessage.Type.ORDER);
			ClientSideMessageDummy msg6 = new ClientSideMessageDummy(ClientSideMessage.Type.ORDER);
			ClientSideMessageDummy msg7 = new ClientSideMessageDummy(ClientSideMessage.Type.ERROR);
			
			Order order1 = Order.createSellOrder("joao", "WER", 10, 2.5);
			order1.setServerOrderID(1);
			msg1.setOrder(order1);
			
			Order order2 = Order.createSellOrder("carlos", "PCG", 70, 2.3);
			order2.setServerOrderID(2);
			msg2.setOrder(order2);
			
			Order order3 = Order.createSellOrder("americo", "JHG", 90, 3.5);
			order3.setServerOrderID(3);
			msg3.setOrder(order3);
			
			Order order4 = Order.createSellOrder("cesar", "GER", 80, 4);
			order4.setServerOrderID(4);
			msg4.setOrder(order4);
			
			Order order5 = Order.createSellOrder("cesar", "GER", 30, 8);
			order5.setServerOrderID(5);
			msg5.setOrder(order5);
			
			Order order6 = Order.createSellOrder("joao", "ERG", 20, 9);
			order6.setServerOrderID(6);
			msg6.setOrder(order6);
			
			msg7.setError("Order not accepted(test).");
			
			clientMessages.add(msg1);
			clientMessages.add(msg2);
			clientMessages.add(msg3);
			clientMessages.add(msg4);
			clientMessages.add(msg5);
			clientMessages.add(msg6);
			clientMessages.add(msg7);
			
			
			
//			dealWithNewMessage(msg1);
//			dealWithNewMessage(msg2);
//			dealWithNewMessage(msg3);
//			dealWithNewMessage(msg4);
//			dealWithNewMessage(msg5);
//			dealWithNewMessage(msg6);
//			dealWithNewMessage(msg7);
			
			
			
			
	}




	@Override
	public void connect(String serverAddress, String nickname) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isConnected() {
		return true;
	}

	@Override
	public void disconnect() {
	}

	@Override
	public boolean hasNextMessage() {
		
		return !clientMessages.isEmpty();
	}

	@Override
	public ClientSideMessage getNextMessage() {
		ClientSideMessageDummy msg = clientMessages.getFirst();
		clientMessages.removeFirst();
		return msg;
	}

	@Override
	public void sendOrder(Order order) {
		
		order.setServerOrderID(100 + (int)(Math.random()*1000));
		System.out.println("id:"+order.getServerOrderID());
		ClientSideMessageDummy newOrderMessage = new ClientSideMessageDummy(ClientSideMessage.Type.ORDER);	
		newOrderMessage.setOrder(order);
		clientMessages.add(newOrderMessage);
		
		
		
	}

}
